#include "SHT30.h"

static const char *TAG = "SHT30.CPP";

void SHT30_DEVICE::begin(uint8_t address,i2c_port_t i2c_num)
{
    _address = address;
    _i2c_num = i2c_num;
}

void SHT30_DEVICE::Reset()
{
    esp_err_t ret;
    ret = i2c_WriteCommand(_i2c_num,_address,
                            0x30,0xa2,true);
    if(ret != ESP_OK)   ESP_LOGW(TAG,"SHT30 Reset ERROR!\r\n");
    else    			ESP_LOGI(TAG,"SHT30 Reset OK!\r\n");
}

esp_err_t SHT30_DEVICE::periodicStart(SHT31D_Repeatability repeatability, SHT31D_Frequency frequency)
{
    esp_err_t ret;
    switch (repeatability)
    {
        case SHT3XD_REPEATABILITY_LOW:
            switch (frequency)
            {
                case SHT3XD_FREQUENCY_HZ5:
                    ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x20,0x2f,true);
                    break;
                case SHT3XD_FREQUENCY_1HZ:
                    ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x21,0x2d,true);
                    break;
                case SHT3XD_FREQUENCY_2HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x22,0x2b,true);
			        break;
		        case SHT3XD_FREQUENCY_4HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x23,0x29,true);
			        break;
		        case SHT3XD_FREQUENCY_10HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x27,0x2a,true);
			        break;
		        default:
			        ret = ESP_FAIL;
			        break;
		    }
		    break;
	    case SHT3XD_REPEATABILITY_MEDIUM:
		    switch (frequency)
		    {
		        case SHT3XD_FREQUENCY_HZ5:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x20,0x24,true);
			        break;
		        case SHT3XD_FREQUENCY_1HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x21,0x26,true);
			        break;
		        case SHT3XD_FREQUENCY_2HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x22,0x20,true);
			        break;
		        case SHT3XD_FREQUENCY_4HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x23,0x22,true);
			        break;
		        case SHT3XD_FREQUENCY_10HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x27,0x21,true);
			        break;
		        default:
			        ret = ESP_FAIL;
			        break;
		    }
		    break;

	    case SHT3XD_REPEATABILITY_HIGH:
		    switch (frequency)
		    {
		        case SHT3XD_FREQUENCY_HZ5:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x20,0x32,true);
			        break;
		        case SHT3XD_FREQUENCY_1HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x21,0x30,true);
			        break;
		        case SHT3XD_FREQUENCY_2HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x22,0x36,true);
			        break;
		        case SHT3XD_FREQUENCY_4HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x23,0x34,true);
			        break;
		        case SHT3XD_FREQUENCY_10HZ:
			        ret = i2c_WriteCommand(_i2c_num,_address,
                                            0x27,0x37,true);
			        break;
		        default:
			        ret = ESP_FAIL;
			        break;
		    }
		    break;
	    default:
		    ret = ESP_FAIL;
		    break; 
    }

    vTaskDelay(100/portTICK_RATE_MS);

	return ret;
    
}

esp_err_t SHT30_DEVICE::periodReadData()
{

    i2c_WriteCommand(_i2c_num,_address,0xe0,0x00,true);
        return  ReadTempeHumi();

}

esp_err_t SHT30_DEVICE::ReadTempeHumi()
{
	esp_err_t ret;

	uint8_t buf[6];
	uint16_t tem,hum;
	float temm,humm;

	vTaskDelay(10/portTICK_RATE_MS);
	ret = i2c_Read_Data(_i2c_num,_address,buf,6);

	// if(ret != ESP_OK)	ESP_LOGW(TAG,"Read SHT30 Data ERROR!\r\n");
	
	tem = (buf[0]<<8) | buf[1];	//温度拼接
	hum = (buf[3]<<8) | buf[4];	//湿度拼接

	temm = 175.0f * tem / 65535.0f - 48.0f; //45.0f
	humm = 100.0f * hum / 65535.0f;

	if((temm>=-20)&&(temm<=125)&&(humm>=0)&&(humm<=100))
	{
		Humi = humm;
		Temp = temm;
	}

	return ret;
}


